package cn.edu.gduf.luowowo.comment.service.impl;

import cn.edu.gduf.luowowo.comment.domain.ScenicComment;
import cn.edu.gduf.luowowo.comment.domain.StrategyComment;
import cn.edu.gduf.luowowo.comment.query.ScenicCommentQuery;
import cn.edu.gduf.luowowo.comment.repository.ScenicCommentRepository;
import cn.edu.gduf.luowowo.comment.service.IScenicCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    private ScenicCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public String saveOrUpdate(ScenicComment comment) {
        //如果是一级评论，二级评论
        if (StringUtils.hasLength(comment.getId())) {
            //判断点论的类型,如果是点评的点评
            if (comment.getType() == ScenicComment.SCENIC_COMMENT_TYPE_COMMENTSON){
                //根据传回的点评的id查询当前的点评
                ScenicComment commentFather=this.get(comment.getId());
                //将点评的回复,设置到当前点评的回复集合中
                //去除回复的id,使其自动生成
                List<ScenicComment> refComment = commentFather.getRefComment();
                comment.setId(UUID.randomUUID().toString().substring(0,23));
                comment.setCreateTime(new Date());
                refComment.add(comment);
                commentFather.setRefComment(refComment);
                repository.save(commentFather);
                return commentFather.getId();
            }
        }
        //添加到数据库中
        comment.setCreateTime(new Date());
        repository.save(comment);
        return comment.getId();
    }

    @Override
    public ScenicComment get(String id) {
        return repository.findById(id).get();
    }

    @Override
    public List<ScenicComment> queryByDetailId(Long scenicId) {
        //TODO:别人都是传qo的，不知道我会不会翻车
        Query query = new Query();
        query.addCriteria(Criteria.where("scenicId").is(scenicId));
        return template.find(query, ScenicComment.class);
    }

    @Override
    public int getCount(Long scenicId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("scenicId").is(scenicId));
        return template.find(query, ScenicComment.class).size();
    }

    @Override
    public List<ScenicComment> queryByCreateTime(int num) {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));
        query.skip(0).limit(num);
        return template.find(query,ScenicComment.class);
    }

    @Override
    public Page queryCommentsByScenicId(ScenicCommentQuery qo) {
        //没有方法可以直接获得Page分页对象，只能够自行封装。类似于PageResult
        //查询的过滤条件
        Query query = new Query();
        if (qo.getScenicId() != -1) {
            //detailId是该攻略文章的id
            query.addCriteria(Criteria.where("scenicId").is(qo.getScenicId()));
        }
        //评论数据总条数   字节码对象是指定mongodb中的集合
        Long count = template.count(query, ScenicComment.class);
        //判断数据总量是否为空，为空返回一个空的Page对象
        if (count == null) {
            return Page.empty();
        }
        //分页的数据，首页，当前页，上一页，下一页之类的 以及排序规则
        //注意它的分页是从0开始的，0表示第一页
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        //查询数据的时候需要分页，需要将Pageable放到query中
        query.with(pageable);
        //当前页的评论数据
        List<ScenicComment> scenicComments = template.find(query, ScenicComment.class);
        //返回分页对象
        return new PageImpl(scenicComments, pageable, count);
    }

    @Override
    public List<ScenicComment> getScenicCommentById(Long id) {
        Query query = new Query();
        Criteria criteria = Criteria.where("author").is(id);
        query.addCriteria(criteria);
        List<ScenicComment> scenicComments = template.find(query, ScenicComment.class);
        return scenicComments;
    }
}
